package bg.code.commons.builder.project;

import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.components.JBTextField;
import com.intellij.ui.components.panels.VerticalBox;
import com.intellij.ui.table.JBTable;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.table.DefaultTableModel;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author 郑兴泉 956607644@qq.com
 * @data 2020/3/18
 * 描述：
 */
public class FileTemplateGroupConfigurableUI {

    private TextFieldWithBrowseButton templatePath;
    private JBTable tamplateTable;
    private VerticalBox root;
    private JTextField userName;
    private JTextField userMail;
    private Project project;
    private ToolConfigurationData configuration;

    public FileTemplateGroupConfigurableUI(Project project, ToolConfigurationData configuration) {
        this.project = project;
        this.configuration = configuration;

        this.tamplateTable = new JBTable();
        this.root = new VerticalBox();
        this.userName = new JBTextField();
        this.userMail = new JBTextField();
        this.templatePath = new TextFieldWithBrowseButton();

        root.add(userName);
        root.add(userMail);
        root.add(templatePath);
        root.add(tamplateTable);

        FileChooserDescriptor fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
        this.templatePath.addBrowseFolderListener("请选择模板目录", "请将模板解压至模板目录中", project, fileChooserDescriptor);
        this.templatePath.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
            @Override
            protected void textChanged(DocumentEvent documentEvent) {
                if (documentEvent.getType().equals(DocumentEvent.EventType.INSERT) ||
                        documentEvent.getType().equals(DocumentEvent.EventType.CHANGE)) {
                    FileTemplateGroupConfigurableUI.this.loadTable();
                }

            }
        });
        this.userName.setText(configuration.getUserName());
        this.userMail.setText(configuration.getUserMail());
        this.templatePath.setText(configuration.getTemplatePath());
        this.tamplateTable.setRowHeight(25);
    }

    private void loadTable() {
        DefaultTableModel model = new DefaultTableModel(new String[]{"模板名称"}, 0);
        Path path = Paths.get(this.templatePath.getText(), new String[0]);
        File[] list = path.toFile().listFiles();
        System.out.println(list);
        for (File file : list) {
            if (!file.isFile()) {
                model.addRow(new String[]{file.getName()});
            }
            System.out.println(file.getName());
        }
        this.tamplateTable.setModel(model);
    }

    public TextFieldWithBrowseButton getTemplatePath() {
        return templatePath;
    }

    public JTable getTamplateTable() {
        return tamplateTable;
    }

    public VerticalBox getRoot() {
        return root;
    }

    public JTextField getUserName() {
        return userName;
    }

    public JTextField getUserMail() {
        return userMail;
    }

    public Project getProject() {
        return project;
    }

    public ToolConfigurationData getConfiguration() {
        return configuration;
    }
}
